package org.example;

import org.example.config.AppConfig;
import org.example.controller.LoginController;
import org.example.controller.model.User;
import org.example.service.LoginService;
import org.example.service.LoginService2;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

    public static void main(String[] args) {
        //根据Spring配置文件路径创建容器：应用上下文对象
        //启动容器：加载xml配置文件，扫描org.example包
        //该包及下边子包中的带Spring注解的类都会注册到容器中，成为Bean对象
        //容器可以简单理解为一个Map结构，key为bean的id或名称，value是bean对象
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        //1.获取Bean对象的第一种方式：类型获取
        LoginController loginController=context.getBean(LoginController.class);
        System.out.println(loginController);

        //2.获取Bean对象的方式二：bean的名称或id获取
        LoginController loginController2= (LoginController) context.getBean("loginController");
        System.out.println(loginController2);
        System.out.println(loginController==loginController2);
        System.out.println();
        //一个类型，可以注册多个bean对象到容器中
        //通过类型获取，容器中只能存在该类型的一个bean,否则就会报错
        //通过名称/id获取，就没有关系
//        LoginController.Tmp tmp=context.getBean(LoginController.Tmp.class);
//        System.out.println(tmp);

        //FactoryBean接口注册的对象
        LoginController.Tmp tmp= (LoginController.Tmp) context.getBean("tmpFactory");
        System.out.println(tmp);
        //注入的依赖
        LoginService loginService=context.getBean(LoginService.class);
        System.out.println(loginService.getTmpFactory());
        //Tmp内部类注册的Bean对象
        LoginController.Tmp tmp2= (LoginController.Tmp) context.getBean("tmp");
        System.out.println(tmp2);
        //注入的依赖
        LoginService _loginService=context.getBean(LoginService.class);
        System.out.println(_loginService.getTmp());


        LoginService2 loginService2=context.getBean(LoginService2.class);
        System.out.println(loginService2.getTmp());


        AppConfig.AppTmp appTmp= (AppConfig.AppTmp) context.getBean("test");
        System.out.println(appTmp.getTmp());


        System.out.println();
        System.out.println("以下是user：");
        User user=context.getBean(User.class);
        User user1= (User) context.getBean("user");
        System.out.println(user);
        System.out.println(user1);

        System.out.println();
        System.out.println("以下是@Configuration注册配置类到容器中：");
        AppConfig appConfig=context.getBean(AppConfig.class);
//        AppConfig appConfig1 = (AppConfig) context.getBean("test");
//        System.out.println(appConfig1);
        System.out.println(appConfig);



        //关闭容器
        ((ClassPathXmlApplicationContext) context).close();
    }
}